/*
 * IO工具 Class
 *
 * VERSION  DATE        BY              REASON
 * -------- ----------- --------------- ------------------------------------------
 * 1.00     2011.11.16  Huyunlin           程序・发布
 * -------- ----------- --------------- ------------------------------------------
 * Copyright 2011 softvan System. - All Rights Reserved.
 *
 */
package cn.com.softvan.common;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

/**
 * <p>IO工具类</p>
 *
 * @author Huyunlin
 */
public class IOHelper extends IOUtils {
	
	private static final transient Logger log = Logger.getLogger(IOHelper.class);
	
	/**
	 * <p>读取文件</p>
	 * @param key
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
    public static synchronized String readFileData(String fileName) throws Exception {
    	
        // 检查文件名是否存在
        if (Validator.isNullEmpty(fileName)) {
        	log.error("没有文件名，读取失败");
            return null;
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader((new FileInputStream(fileName)), "UTF-8"));

		try {
			
			StringBuilder data = new StringBuilder();
			String line = null;
			while(( line = reader.readLine())!=null){
				data.append(line);
			}
			reader.close();
			return data.toString();
		} catch (Exception e){
			if (reader != null) {
				reader.close();
			}
			log.error("读取文件失败",e);
			return null;
		}
    }
}
